using System;
using System.Collections;

namespace GURUCORE.Framework.DataAccess.ORMapping.CriteriaMapping
{
	public class Order
	{
		public const int ASC = 1;
		public const int DESC = -1;

		private Expression m_oExpression;
		private int m_nSort;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_oExpressionToOrder">What to order by</param>
        /// <param name="p_nint">Order.ASC or Order.DESC</param>
		public Order(Expression p_oExpressionToOrder, int p_nOrient)
		{
            m_oExpression = p_oExpressionToOrder;
            m_nSort = p_nOrient;
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_oExpressionToOrder">What to order by</param>
        public Order(Expression p_oExpressionToOrder)
        {
            m_oExpression = p_oExpressionToOrder;
            m_nSort = ASC;
        }


        public Order(string p_sFieldToOrder, int p_nOrient)
            : this (new Expression(p_sFieldToOrder) , p_nOrient)
        {}

        public Order(string p_sFieldToOrder)
            : this(new Expression(p_sFieldToOrder), Order.ASC)
        {}


		public Order ReversedOrder
		{
			get
			{
				return new Order(m_oExpression,-m_nSort);
			}
		}

		public string ToExpressionString(IExpressionMaker p_oExpressionMaker)
		{
			string sResult = string.Empty;

			switch (m_nSort)
			{
				case ASC :
				{
					sResult = m_oExpression.ToExpressionString(p_oExpressionMaker) + " ASC";
					break;
				}
				case DESC :
				{
					sResult = m_oExpression.ToExpressionString(p_oExpressionMaker) + " DESC";
					break;
				}
				default:
				{
					sResult = m_oExpression.ToExpressionString(p_oExpressionMaker) + " ASC";
					break;
				}
			}

			return sResult;
		}

		public Expression Expression
		{
			get
			{
				return m_oExpression;
			}
		}

		public string Sort
		{
			get
			{
				if (m_nSort > 0)
				{
					return "ASC";
				}
				else
				{
					return "DESC";
				}
			}
		}
	}
}
